<template>
	<view class="flex-col page">

		<view class="flex-col flex-1 group_2">
			<view class="shrink-0 section_2"></view>

			<view class="flex-col relative group_3">
				<image class="image_4" v-if="type !=3 || home =='Japanese' " src="/static/icon/bf.png" />
				<view class="mt-12 flex-col section_3">
					<view class="flex-col group_4" v-if="type !=3 || home =='Japanese'  ">
						<view class="flex-row items-center group_5">
							<view class="flex-col justify-start items-center image-wrapper">
								<image class="image_5" src="/static/icon/yjxd.png" />
							</view>
							<view class="section_4 view"></view>
							<view class="flex-col justify-start items-center image-wrapper_2">
								<image class="image_6" src="/static/icon/gwsm.png" />
							</view>
							<view class="section_4 view_2"></view>
							<view class="flex-col justify-start items-center image-wrapper_3">
								<image class="image_7" src="/static/icon/qwdb.png" />
							</view>
							<view class="section_4 view_3"></view>
							<view class="flex-col justify-start items-center image-wrapper_5">
								<image class="image_9" src="/static/icon/aqby.png" />
							</view>
							<view class="section_4 view_4"></view>
							<view class="flex-col justify-start items-center image-wrapper_4">
								<image class="image_8" src="/static/icon/xjhy.png" />
							</view>
						</view>

						<view class="flex-row justify-between mt-11-5">
							<text class="font_2">一键下单</text>
							<text class="font_2 text_5">顾问上门</text>
							<text class="font_2 text_6">全屋打包</text>
							<text class="font_2">安全搬运</text>
							<text class="font_2 text_7">新家还原</text>
						</view>
					</view>

					<My-Storage ref="storagese" v-show="type == 3"></My-Storage>
					<My-movehouse v-show="type!= 3" ref="vehouses"></My-movehouse>


					<view class="flex-col group_6 ">
						<view class="self-center">
							<text class="text_14">{{its.initial_price}}</text>
							<text class="text_15">元</text>
						</view>
						<text class="self-center font_4 text_16">{{its.price_introduction}}</text>
						<view class="flex-col justify-start items-center button text-wrapper_3" @click="evaluation">
							<text class="text_17">预约上门评估</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="tab-bar"></view>
	</view>
</template>

<script>
	import {
		CreateOrder
	} from '@/utils/user.js'
	import Storage from '@/components/Storage.vue';
	import movehouse from '@/components/movehouse.vue';
	export default {
		name: "JapaneseMoving",
		components: {
			'My-Storage': Storage,
			'My-movehouse': movehouse
		},
		data() {
			return {
				type: 1,
				its: {},
				home: '',
				ti: 1,
			};
		},
		methods: {


			tiono(data) {


				var LoginData = uni.getStorageSync('LoginData');


				console.log(LoginData)

				if (LoginData) {

				} else {
					uni.showToast({
						title: '请先登录',
						icon: 'none',
						duration: 2000
					})
					setTimeout(function() {
						uni.switchTab({
							url: '/pages/Mine/Mine'
						});
						console.log('两秒后执行的函数');
					}, 2000);


					return

				}






				// moving_type	string	是	搬家类型 1normal 2japanese 3company（传数字即可）
				// select_type	string	是	二级类型 1小面 2金杯 3依维柯 4箱货 5全日式 6 半日式 7 全屋收纳整理（传数字即可）
				// user_name	string	是	客户姓名
				// user_mobile	string	是	客户手机号
				// out_address	string	是	搬出地址
				// in_address_json	json	是	搬入地址 json “[“北京市西城区西直门大街345号”,”北京市大兴区庞各庄幸福街345号”]”
				// moving_time	int	是	上门时间 时间戳
				// init_amount	float	是	初始金额
				console.log(data)


				var _this = this

				CreateOrder(data).then(res => {
					if (res.data.code == 0) {


						setTimeout(function() {

							if (data.moving_type == 2) {
								var key = "rishi";
								// 清空本地缓存中与 key 相关的数据

								uni.removeStorageSync(key);
								console.log("本地缓存已清空");
								const loginComponent = _this.$refs.vehouses;
								// 调用子组件方法
								loginComponent.uetdata()
							}


						}, 2000);





						if (data.select_type == 7) {
							var key = "qwzlsn";
							// 清空本地缓存中与 key 相关的数据
							uni.removeStorageSync(key);
							   
							const loginComponent = this.$refs.storagese;
							   
							loginComponent.uentestay();

						}


						uni.showToast({
							title: '订单已成功创建',
							icon: 'none',
							duration: 2500
						})
						   
				

						const params = {
							id: res.data.data,
							pick:data.init_amount
						};
								   


						   

						setTimeout(function() {
							// 在这里写要执行的函数体

							// 将 params 对象转换为查询字符串
							const queryString =
								`id=${encodeURIComponent(params.id)}&pick=${encodeURIComponent(params.pick)}`;
							uni.navigateTo({
								url: '/pages/Orderconfirmation/Orderconfirmation?' + queryString
							});


							return


							console.log('两秒后执行的函数');
						}, 2000);

						// setTimeout(function() {
						// 	uni.navigateTo({
						// 		url: '/pages/Orderconfirmation/Orderconfirmation?id=' + res.data.data
						// 	})
						// 	return
						// 	uni.switchTab({
						// 		url: '/pages/orderlist/orderlist'
						// 	});
						// 	console.log('两秒后执行的函数');
						// }, 2000);



					} else {

						uni.showToast({
							title: res.data.msg,
							icon: 'none',
							duration: 2000
						})

					}



				}).catch(error => {
					console.error('Init 方法执行出错：', error);
				});

			},



			// 预约上门评估
			evaluation() {
				console.log(this.type, this.its)

				// 判断 type = 1 的时候是日式搬家  == 3 的时候 就是 全屋整理
				if (this.type == 1) {


					var key = "rishi";

					var storage = uni.getStorageSync(key) || {};


					if (!storage.onew) {


						uni.showToast({
							title: '请输入搬出地址',
							icon: 'none',
							duration: 2000
						})

					}


					// if (!storage.two) {
					// 	uni.showToast({
					// 		title: '请输入搬入地址',
					// 		icon: 'none',
					// 		duration: 2000
					// 	})
					// 	return
					// }
					if (!storage.name) {


						uni.showToast({
							title: '请输入联系人',
							icon: 'none',
							duration: 2000
						})
						return
					}
					if (!storage.time) {

						uni.showToast({
							title: '请输入时间',
							icon: 'none',
							duration: 2000
						})
						return
					}



					if (storage.phone == '') {
						uni.showToast({
							title: '手机号码为空',
							icon: 'none',
							duration: 2000
						})
						return
					}
					const regex = /^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/;

					if (regex.test(storage.phone)) {

					} else {
						console.log("手机号码格式不正确");
						uni.showToast({
							title: '手机号码有误',
							icon: 'none',
							duration: 2000
						})
						return
					}




					// 判断
					if (this.its.name == '全日式') {

						var select_type = 5

					} else {

						var select_type = 6


					}



					// 定义键名数组
					var keysToCheck = ['two', 'three', 'four', 'five'];

					// five

					// 创建一个空数组来存储值
					var valuesArray = [];

					// 遍历键名数组，检查 storage 中是否有对应的项，如果有则将值添加到数组中
					keysToCheck.forEach(function(key) {

						if (storage.hasOwnProperty(key)) {
							valuesArray.push(storage[key]);
						}
					});

					console.log(valuesArray, 88888)


					// 11111111111111
					var data = {
						moving_type: 2,
						select_type: select_type,
						user_name: storage.name,
						user_mobile: storage.phone,
						out_address: storage.onew,
						in_address_json: valuesArray,
						moving_time: storage.time.moving_time,
						init_amount: this.its.initial_price,
					}

					console.log(data, 'data')

					this.tiono(data)


				} else {


					var key = "qwzlsn";

					var storage = uni.getStorageSync(key) || {};

					console.log(storage)


					if (!storage.onew) {


						uni.showToast({
							title: '请输入搬出地址',
							icon: 'none',
							duration: 2000
						})

					}

					if (!storage.name) {


						uni.showToast({
							title: '请输入搬出姓名',
							icon: 'none',
							duration: 2000
						})

					}


					if (!storage.time) {

						uni.showToast({
							title: '请输入时间',
							icon: 'none',
							duration: 2000
						})
						return
					}



					if (storage.phone == '') {
						uni.showToast({
							title: '手机号码为空',
							icon: 'none',
							duration: 2000
						})
						return
					}
					const regex = /^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/;

					if (regex.test(storage.phone)) {

					} else {
						console.log("手机号码格式不正确");
						uni.showToast({
							title: '手机号码有误',
							icon: 'none',
							duration: 2000
						})
						return
					}




					// 定义键名数组
					var keysToCheck = ['onew'];

					// five

					// 创建一个空数组来存储值
					var valuesArray = [];

					// 遍历键名数组，检查 storage 中是否有对应的项，如果有则将值添加到数组中
					keysToCheck.forEach(function(key) {

						if (storage.hasOwnProperty(key)) {
							valuesArray.push(storage[key]);
						}
					});






					var data = {
						moving_type: 1,
						select_type: 7,
						user_name: storage.name,
						user_mobile: storage.phone,
						out_address: storage.onew,
						in_address_json: valuesArray,
						// in_address_json:storage.two,
						moving_time: storage.time.moving_time,
						init_amount: this.its.initial_price,
					}


					this.tiono(data)




				}





			},

			uentestay() {

				const loginComponent = this.$refs.storagese;

				loginComponent.uentestay();
			},

			vehouse() {
				this.$nextTick(() => {
					// 通过 $refs 获取子组件实例
					const loginComponent = this.$refs.vehouses;
					if (loginComponent) {
						var i = "index"
						var e = this.type
						var its = 0
						var ti = this.ti

						// 调用子组件方法
						loginComponent.open(i, e, its, ti);
					}
				});
			},
			// 传参过来
			// index  Japanese
			// ti  全日式 半日式  全屋收纳整理
			//    1     2        3
			open(i, e, its, ti) {

				console.log(i, e, its, ti)
				this.home = i
				this.type = e
				this.its = its
				this.ti = ti

				if (this.type == 1) {
					if (ti == 2) {
						this.type = 3

						this.uentestay()
					}

				}
				this.vehouse()
			},
		}
	}
</script>

<style lang="less" scoped>
	.mt-11-5 {
		margin-top: 23rpx;
	}

	.ml-8-5 {
		margin-left: 17rpx;
	}

	.ml-9-5 {
		margin-left: 19rpx;
	}

	.page {
		background-color: #f4f5f7;
		width: 100%;
		overflow-y: auto;
		overflow-x: hidden;
		height: 100%;

		.group_2 {
			padding-bottom: 20rpx;
			overflow-y: auto;

			.section_2 {
				background-image: linear-gradient(180deg, #ffffff 0%, #ffffff00 100%);
				height: 390rpx;
			}

			.group_3 {
				margin-top: -380rpx;
				padding: 0 12rpx;

				.image_4 {
					border-radius: 12rpx;
					width: 92vw;
					margin: 0 auto;
					height: 41.2vw;
					// border: 2px solid red;
				}

				.section_3 {
					padding: 0 17.18rpx 40rpx 20.2rpx;
					background-color: #ffffff;
					border-radius: 16rpx;

					.group_4 {
						padding: 40rpx 0 45.4rpx;

						.group_5 {
							padding: 0 23.8rpx;

							.image-wrapper {
								flex: 1 1 45.4rpx;

								.image_5 {
									border-radius: 4rpx;
									width: 30rpx;
									height: 40rpx;
								}
							}

							.section_4 {
								flex: 1 1 45.4rpx;
								background-color: #999999;
								height: 2.4rpx;
							}

							.view {
								margin-left: 33.6rpx;
							}

							.image-wrapper_2 {
								margin-left: 28.6rpx;
								flex: 1 1 45.4rpx;

								.image_6 {
									width: 32rpx;
									height: 37rpx;
								}
							}

							.view_2 {
								margin-left: 20.6rpx;
							}

							.image-wrapper_3 {
								margin-left: 34.6rpx;
								flex: 1 1 45.4rpx;

								.image_7 {
									width: 36rpx;
									height: 36rpx;
								}
							}

							.view_3 {
								margin-left: 27.6rpx;
							}

							.image-wrapper_5 {
								margin-left: 25.6rpx;
								flex: 1 1 45.4rpx;

								.image_9 {
									width: 39.3rpx;
									height: 32.8rpx;
								}
							}

							.view_4 {
								margin-left: 27.6rpx;
							}

							.image-wrapper_4 {
								margin: 0 2.82rpx 3.1rpx 31.4rpx;
								flex: 1 1 45.4rpx;

								.image_8 {
									width: 38rpx;
									height: 36rpx;
								}
							}
						}

						.font_2 {
							font-size: 24rpx;
							font-family: PingFang SC;
							line-height: 22.26rpx;
							color: #666666;
						}

						.text_5 {
							line-height: 22.42rpx;
						}

						.text_6 {
							line-height: 22.4rpx;
						}

						.text_7 {
							line-height: 22.4rpx;
						}
					}

					.section_6 {
						padding: 34rpx 22.1rpx 34rpx 24rpx;
						background-color: #f4f5f7;
						border-radius: 8rpx;

						.text-wrapper {
							padding: 6.52rpx 0 6.92rpx;
							background-color: #2b8eff;
							border-radius: 8rpx;
							width: 32rpx;
							height: 32rpx;
						}

						.text_8 {
							line-height: 22.4rpx;
						}

						.text_9 {
							line-height: 22.14rpx;
						}

						.text-wrapper_2 {
							padding: 6.62rpx 0 6.78rpx;
							background-color: #ff772b;
							border-radius: 8rpx;
							width: 32rpx;
							height: 32rpx;

							.text_10 {
								line-height: 18.6rpx;
							}
						}

						.font_3 {
							font-size: 20rpx;
							font-family: PingFang SC;
							line-height: 18.56rpx;
							color: #ffffff;
						}
					}

					.section_5 {

						// margin-left: 19.8rpx;
						// margin-right: 22.82rpx;
					}

					.group_6 {
						padding: 0 19.8rpx;

						.view_7 {
							margin-right: 3.02rpx;
						}

						.section_1 {
							margin-right: 3.02rpx;
							padding: 36rpx 22.1rpx 36rpx 26rpx;
							background-color: #f4f5f7;
							border-radius: 8rpx;
						}

						.section_9 {
							margin-right: 3.02rpx;
							padding: 36rpx 21.96rpx 36rpx 26rpx;
							background-color: #f4f5f7;
							border-radius: 8rpx;

							.text_12 {
								line-height: 22.08rpx;
							}

							.text_13 {
								line-height: 22.18rpx;
							}
						}

						.image_10 {
							width: 28rpx;
							height: 28rpx;
						}

						.text_14 {
							color: #222222;
							font-size: 64rpx;
							font-family: PingFang SC;
							line-height: 47.5rpx;
						}

						.text_15 {
							color: #222222;
							font-size: 32rpx;
							font-family: PingFang SC;
							line-height: 28.1rpx;
						}

						.text_16 {
							margin-top: 24.92rpx;
							line-height: 23.58rpx;
						}

						.button {
							align-self: stretch;
							margin-right: 3.02rpx;
							margin-top: 20.12rpx;

							.text_17 {
								color: #ffffff;
								font-size: 36rpx;
								font-family: PingFang SC;
								line-height: 33.66rpx;
							}
						}

						.text-wrapper_3 {
							padding: 28.12rpx 0 28.22rpx;
							background-color: #2b8eff;
							border-radius: 8rpx;
						}
					}

					.view_5 {
						margin-top: 32rpx;
					}

					.font_4 {
						font-size: 24rpx;
						font-family: PingFang SC;
						line-height: 22.26rpx;
						color: #222222;
					}

					.font_5 {
						font-size: 24rpx;
						font-family: PingFang SC;
						line-height: 22.26rpx;
						color: #999999;
					}

					.view_6 {
						margin-top: 61.4rpx;
					}
				}
			}
		}

		.tab-bar {
			height: 16.26rpx;
		}
	}
</style>